<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Setseccode extends CI_Controller{

  public function __construct()
  {
    parent::__construct();
    //Codeigniter : Write Less Do More
    $this->load->model('User_model', 'users');
    $this->load->model('Operation_model','oper');
    if(empty($_SESSION['id'])){
      echo "0";
      echo "<script src='/static/js/jquery.min.js?v=2.1.4'></script>";
      echo "<script src='/static/js/layer/layer.js'></script>";
      echo "<script>layer.msg('你还没有登录，请先登录',{icon:2});setTimeout(function(){window.location.href='/index.php/Login';},1500)</script>";
      exit;
    }
    
  }

  function index()
  {
      $this->load->view('setseccode');
  }

  //修改安全密码
  public function editSeccode(){
  	$this->load->view('editseccode');
  }

  // public function getSmsCode(){

  //   if(empty($_SESSION['id'])){
  //     jechof("您还没有登录，请先登录！");
  //   }else{
  //     jecho($this->sms->getSmsCode($_SESSION['phone']));
  //   }
  // }

  public function editChangeSeccodePwd(){
    if(empty($_SESSION['seccode'])){
      echo "0";
      echo "<script src='/static/js/jquery.min.js?v=2.1.4'></script>";
      echo "<script src='/static/js/layer/layer.js'></script>";
      echo "<script>layer.msg('你还没有设置安全密码，请先设置安全密码',{icon:2});setTimeout(function(){window.location.href='/index.php/Setseccode';},1500)</script>";
      exit;
    }
    $password = $this->input->post('password');
    
    if($this->users->changeUserInfo(array('id'=>$_SESSION['id']),array('seccode'=>substr(md5($this->input->post('password')), 8, 16)))){
      //$_SESSION['seccode'] = substr(md5($this->input->post('password')), 8, 16);
      $this->oper->add(array(
        'operation'=>'修改安全密码成功',
        'ip'=> $this->input->ip_address(),
        'uid'=>$_SESSION['id'],
        'time'=> date('Y-m-d h:i:s',time())
      ));
      //更新SESSION
      $_SESSION = $this->users->getUserInfo(array('a.id'=>$_SESSION['id']));
      jechos("修改安全密码成功");
       
    }else{
      $this->oper->add(array(
        'operation'=>'修改安全密码失败',
        'ip'=> $this->input->ip_address(),
        'uid'=>$_SESSION['id'],
        'time'=> date('Y-m-d h:i:s',time())
      ));
      jechof("修改安全密码失败");
    }
  }
  

}
